//package com.geektime.hello.entity.es;
//
//import org.elasticsearch.common.lucene.search.function.FunctionScoreQuery;
//import org.elasticsearch.index.query.QueryBuilder;
//import org.elasticsearch.index.query.QueryBuilders;
//import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
//import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
//import org.elasticsearch.index.search.MatchQuery;
//import org.elasticsearch.search.sort.SortBuilders;
//import org.elasticsearch.search.sort.SortOrder;
//import org.springframework.data.domain.Page;
//import org.springframework.data.domain.PageRequest;
//import org.springframework.data.domain.Pageable;
//import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
//import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
//import org.springframework.util.StringUtils;
//
//import static org.elasticsearch.index.query.QueryBuilders.matchQuery;
//
///**
// * @Author DRevelation
// * @Date 2020-07-08 下午 14:20
// * @Desc
// */
//public interface ProductRepository extends ElasticsearchRepository<ESProduct, Integer> {
//
//    default Page<ESProduct> search(Integer cid, String keyword, Pageable pageable) {
//        NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
//        if (cid != null) {
//            nativeSearchQueryBuilder.withFilter(QueryBuilders.termQuery("cid", cid));
//        }
//        if (StringUtils.hasText(keyword)) {
//            FunctionScoreQueryBuilder.FilterFunctionBuilder[] functions = { // TODO 芋艿，分值随便打的
//                    new FunctionScoreQueryBuilder.FilterFunctionBuilder(matchQuery("name", keyword),
//                            ScoreFunctionBuilders.weightFactorFunction(10)),
//                    new FunctionScoreQueryBuilder.FilterFunctionBuilder(matchQuery("sellPoint", keyword),
//                            ScoreFunctionBuilders.weightFactorFunction(2)),
//                    new FunctionScoreQueryBuilder.FilterFunctionBuilder(matchQuery("categoryName", keyword),
//                            ScoreFunctionBuilders.weightFactorFunction(3)),
////                    new FunctionScoreQueryBuilder.FilterFunctionBuilder(matchQuery("description", keyword),
////                            ScoreFunctionBuilders.weightFactorFunction(2)), // TODO 芋艿，目前这么做，如果商品描述很长，在按照价格降序，会命中超级多的关键字。
//            };
//            FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery(functions)
//                    .scoreMode(FunctionScoreQuery.ScoreMode.SUM)
//                    .setMinScore(2F);
//            nativeSearchQueryBuilder.withQuery(functionScoreQueryBuilder);
//        }
//        //排序
//        if (StringUtils.hasText(keyword)) {
//            //关键字，使用打分
//            nativeSearchQueryBuilder.withSort(SortBuilders.scoreSort().order(SortOrder.DESC));
//        } else if (pageable.getSort().isSorted()) {
//            //有排序，则进行拼接
//            pageable.getSort().get().forEach(sortField -> nativeSearchQueryBuilder.withSort(SortBuilders.fieldSort(sortField.getProperty())
//                    .order(sortField.getDirection().isAscending() ? SortOrder.ASC : SortOrder.DESC)));
//        } else { // <3.3> 无排序，则按照 ID 倒序
//            nativeSearchQueryBuilder.withSort(SortBuilders.fieldSort("id").order(SortOrder.DESC));
//        }
//        // <4> 分页
//        // 避免
//        nativeSearchQueryBuilder.withPageable(PageRequest.of(pageable.getPageNumber(), pageable.getPageSize()));
//        // <5> 执行查询
//        return search(nativeSearchQueryBuilder.build());
//    }
//}
